/**
 * @param {string} s
 * @return {number}
 */
var firstUniqChar = function(s) {
  const count = new Array(26).fill(0), order = new Array(26).fill({index: -1, position: -1});
  let index = 0;
  for(let i = 0; i < s.length; i++) {
    count[s[i].charCodeAt() - 97]++;
    // console.log(s[i], s[i].charCodeAt() - 97);
    if(count[s[i].charCodeAt() - 97] >= 2) {
      order[s[i].charCodeAt() - 97] = {
        index: -1
      };
    } else {
      index++;
      let obj = {
        index,
        position: i
      }
      order[s[i].charCodeAt() - 97] = obj;
    }
  }


  let minIndex = 0, minValue = order[0].index;
  for(let i = 0; i < order.length; i++) {
    if(order[i].index == -1)
      continue;
    else {
      if(minValue == -1) {
        minValue = order[i].index;
        minIndex = i;
      } else if(order[i].index != -1 && minValue > order[i].index) {
        minValue = order[i].index;
        minIndex = i;
      }
    }
  }
  // console.log(minIndex, minValue, order[minIndex].position);
  if(order[minIndex].position != undefined)
    return order[minIndex].position;
  return -1;
};

let s = "leetcode";
console.log(firstUniqChar(s));
s = "loveleetcode";
console.log(firstUniqChar(s));
s = "aabb";
console.log(firstUniqChar(s));
